/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.display;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;

import com.android.tv.settings.R;
import com.android.tv.settings.dialog.DialogFragment;
import com.android.tv.settings.dialog.DialogFragment.Action;
import android.os.SystemProperties;

import java.util.ArrayList;

/**
 * Activity that allows the enabling and disabling of ProjMode effects.
 */
public class ProjModeActivity extends Activity implements Action.Listener {

    private static final String PREFERENCE_KEY = "display";
    private static final String ACTION_DESK_ZT = "desk_zt_1";
    private static final String ACTION_DESK_BT = "desk_bt_0";
    private static final String ACTION_DZ_ZT = "dz_zt_3";
    private static final String ACTION_DZ_BT = "dz_bt_2";	
	private static final String ACTION_ZD_ZT = "dz_bt_4";
	private static final String ACTION_ZD_BT = "dz_bt_5";
    private DialogFragment mDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialogFragment = new DialogFragment.Builder()
                .title(getString(R.string.proj_mode))
                .breadcrumb(getString(R.string.device_display))
                .iconResourceId(getIconResource(getContentResolver()))
                .iconBackgroundColor(getResources().getColor(R.color.icon_background))
                .actions(getActions()).build();
		//DialogFragment.setBackground(getResources().getDrawable(R.drawable.gradient_settings)); //Blues
        DialogFragment.add(getFragmentManager(), mDialogFragment);
    }

    private ArrayList<Action> getActions() {
        String ProjModeAreOn = getProjectMode( );
        ArrayList<Action> actions = new ArrayList<Action>();
        actions.add(new Action.Builder()
                .key(ACTION_DESK_ZT)
                .title(getString(R.string.proj_deskzt))
                .checked(ACTION_DESK_ZT.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_DESK_BT)
                .title(getString(R.string.proj_deskbt))
                .checked(ACTION_DESK_BT.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
		actions.add(new Action.Builder()
                .key(ACTION_DZ_ZT)
                .title(getString(R.string.proj_dzzt))
                .checked(ACTION_DZ_ZT.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_DZ_BT)
                .title(getString(R.string.proj_dzbt))
                .checked(ACTION_DZ_BT.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
	/*	actions.add(new Action.Builder()
                .key(ACTION_ZD_ZT)
                .title(getString(R.string.proj_zdzt))
                .checked(ACTION_ZD_ZT.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_ZD_BT)
                .title(getString(R.string.proj_zdbt))
                .checked(ACTION_ZD_BT.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());		*/
        return actions;
    }

    @Override
    public void onActionClicked(Action action) {
        if (ACTION_DESK_ZT.equals(action.getKey())) {
        	setProjectMode("1");
        } else if (ACTION_DESK_BT.equals(action.getKey())) {
        	setProjectMode("0");
        }else if (ACTION_DZ_ZT.equals(action.getKey())) {
        	setProjectMode("3");
        }else if (ACTION_DZ_BT.equals(action.getKey())) {
        	setProjectMode("2");
        }else if (ACTION_ZD_ZT.equals(action.getKey())) {
        	setProjectMode("4");
        }else if (ACTION_ZD_BT.equals(action.getKey())) {
        	setProjectMode("5");
        }
        mDialogFragment.setIcon(getIconResource(getContentResolver()));
    }

    public static String getPreferenceKey() {
        return PREFERENCE_KEY;
    }

    public static int getIconResource(ContentResolver contentResolver) {
        return R.drawable.ic_settings_display;//need to do replace ico Blues
    }

    private void setProjectMode(String mValue) {
        SystemProperties.set("persist.dlp.mode", mValue);
		SystemProperties.set("persist.dlp.update", "2"); //2 is setprojectmode
    }

    private static String getProjectMode( ) {
        return SystemProperties.get("persist.dlp.mode", "1");
    }

	public static int getProjectModeDesc( ) {
        String tmp =  getProjectMode( );
		if (tmp.equals("0"))
		{
			return R.string.proj_deskbt;
		}
		else if (tmp.equals("1"))
		{
			return R.string.proj_deskzt;
		}
		else if (tmp.equals("2"))
		{
			return R.string.proj_dzbt;
		}
		else if (tmp.equals("3"))
		{
			return R.string.proj_dzzt;
		}
		return R.string.proj_deskzt;
    }
		
}
